package com.thread.exe;

public abstract class BaseBoundedBuffer<V> {
	private final V[] buf;
	private int tail;
	private int head;
	private int count;
	
	@SuppressWarnings("unchecked")
	protected BaseBoundedBuffer(int capacity) {
		this.buf = (V[]) new Object[capacity];
	}
	
	protected synchronized final void doPut(V v) {
		buf[tail] = v;
		if(++tail == buf.length) 
			tail = 0;
		++count; //因为子类外层有对空间的判断，所以不会发生。
	}
	
	protected synchronized final V doTake() {
		V v = buf[head];
		if(++head == buf.length) 
			head = 0;
		--count; //因为子类外层有对空间的判断，所以不会发生。
		return v;
	}
	
	public synchronized final boolean isFull() {
		return count == buf.length;
	}
	
	public synchronized final boolean isEmpty() {
		return count == 0;
	}
}
